package com.tan.test;

import com.tan.po.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

public class MybatisHelloWorld {
    @Test
    public void helloWorldTest() throws Exception{
        //1.加载配置 由于sqlMapConfig.xml引入了mapper.xml所以此处就加载了所有配置文件
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //2.获得sqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //3.获得连接
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //4.执行sql语句 第一个参数是名称空间，mappedStatementId，第二个参数输入参数的值
        User user = sqlSession.selectOne("userMapper.queryUserById", 1);
        System.out.println(user);
        //5.关闭资源
        sqlSession.close();
        inputStream.close();
    }
}
